package net.csdra.libraryborrowing.service.user;

import net.csdra.libraryborrowing.domain.user.*;
import net.csdra.libraryborrowing.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.nio.file.AccessDeniedException;
import java.sql.Timestamp;
import java.util.Date;

/**
 * Created by add on 2017/10/17.
 */
public class UserBorrowServiceImpl implements UserBorrowService {

    UserBorrowRepository userBorrowRepository;
    CurrentUserInfo currentUserInfo;
    TransferBorrowInfoComponent transferBorrowInfoComponent;
    UserBorrowLogComponent userBorrowLogComponent;
    @Autowired
    public UserBorrowServiceImpl(UserBorrowRepository userBorrowRepository, CurrentUserInfo currentUserInfo, TransferBorrowInfoComponent transferBorrowInfoComponent) {
        this.userBorrowRepository = userBorrowRepository;
        this.currentUserInfo = currentUserInfo;
        this.transferBorrowInfoComponent = transferBorrowInfoComponent;
    }

    @Override
    @Transactional
    public UserBorrow redecorate(Redecorate redecorate) throws AccessDeniedException {
        Assert.notNull(redecorate, "@参数不能为空");
        Assert.hasLength(redecorate.getUserBorrowId(), "BorrowId不能为空");
        Assert.notNull(redecorate.getRedecorateDate(), "借阅时间不能为空");
        Assert.notNull(redecorate.getRemark(), "备注不能为空");
        User currentUser = this.currentUserInfo.get();
        String currentId = currentUser.getId();
        UserBorrow userBorrow = this.userBorrowRepository.findByMetaIdAndId(currentId, redecorate.getUserBorrowId());
        Assert.notNull(userBorrow, "@借阅记录不存在");
        Long localTmie = new Date().getTime();
        Assert.state(redecorate.getRedecorateDate().getTime() > userBorrow.getEnd().getTime() &&
                localTmie < redecorate.getRedecorateDate().getTime(), "@借阅时间不符合要求");
        Assert.state(userBorrow.getState().equals(UserBorrowState.BORROWING) ||
                userBorrow.getState().equals(UserBorrowState.OVERDUE), "@书籍状态不符合");
        userBorrow.setEnd(new Timestamp(redecorate.getRedecorateDate().getTime()));
        this.userBorrowRepository.save(userBorrow);
        UserBorrow result = new UserBorrow();
        Utils.copy(userBorrow, result);

        UserBorrowLog userBorrowLog = Utils.createAutoSet(UserBorrowLog.class, currentUser.getUserName());
        userBorrowLog.setMetaId(userBorrow.getId());
        userBorrowLog.setBegin(userBorrow.getBegin());
        userBorrowLog.setEnd(userBorrow.getEnd());
        userBorrowLog.setRemark(redecorate.getRemark());
        this.userBorrowLogComponent.addUserBorrowLog(userBorrowLog);
        return result;
    }

    @Override
    @Transactional
    public UserBorrow handleTransferBorrowInfo(HandleTransferBorrowInfo handleTransferBorrowInfo) throws AccessDeniedException {
        Assert.notNull(handleTransferBorrowInfo, "@参数不能为空");
        Timestamp beginTime = new Timestamp(System.currentTimeMillis());
        TransferBorrowInfo transferBorrowInfo = this.transferBorrowInfoComponent.getTransferBorrowInfo(
                handleTransferBorrowInfo.getTransferBorrowInfoId()
        );
        Assert.notNull(transferBorrowInfo, "@转借记录为空");
        User currentUser = this.currentUserInfo.get();
        String currId = currentUser.getId();
        Assert.state(transferBorrowInfo.getTargetUserId().equals(currId) &&
                transferBorrowInfo.getState().equals(TransferBorrowInfoState.READY), "@转借条件或者用户ID不正确");
        // TODO: 创建对象
        UserBorrow userBorrow = this.userBorrowRepository.findOne(transferBorrowInfo.getMetaId());
        UserBorrow newUserBorrow = Utils.createAutoSet(UserBorrow.class, currentUser.getUserName());
        newUserBorrow.setMetaId(currId);
        newUserBorrow.setBookCopyId(userBorrow.getBookCopyId());
        newUserBorrow.setBookInfoId(userBorrow.getBookCopyId());
        newUserBorrow.setState(UserBorrowState.TRANSFERBORROW);
        //TODO: 待確認
        newUserBorrow.setBegin(null);
        newUserBorrow.setEnd(null);
        this.userBorrowRepository.save(newUserBorrow);

        Timestamp lastTime = new Timestamp(System.currentTimeMillis());
        UserBorrowLog userBorrowLog = Utils.createAutoSet(UserBorrowLog.class, currentUser.getUserName());
        userBorrowLog.setBegin(beginTime);
        userBorrowLog.setEnd(lastTime);
        userBorrowLog.setRemark(beginTime+"-"+lastTime+" "+currentUser.getUserName()+"： 新建了一条记录");
        this.userBorrowLogComponent.addUserBorrowLog(userBorrowLog);

        UserBorrow result = new UserBorrow();
        Utils.copy(newUserBorrow, result);
        return result;
    }
}
